/*********************
 *      INCLUDES
 *********************/
#include "lv_port.h"
#include "board.h"
#include "components\lcd_drivers\lcd.h"
#include "components\lvgl\lvgl.h"
#include "polsys.h"
#include <stdbool.h>

/*********************
 *      DEFINES
 *********************/
#define MY_DISP_HOR_RES    320

#define MY_DISP_VER_RES    240

#define DISP_BUFF_SIZE      (MY_DISP_HOR_RES * 24)      // 单位像素点(lv_color_t)

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void DispFlush(lv_disp_drv_t * mDispDrv, const lv_area_t * area, lv_color_t * color_p);
//static void gpu_fill(lv_disp_drv_t * mDispDrv, lv_color_t * dest_buf, lv_coord_t dest_width,
//        const lv_area_t * fill_area, lv_color_t color);
static void ReadKeyPad(lv_indev_drv_t * drv, lv_indev_data_t*data);
static u8 KeyConvert(key_value_t key);
static void LvglLoop();

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_disp_draw_buf_t mDrawBufStruct;
static lv_color_t mBuff1[DISP_BUFF_SIZE];                        /*A buffer for 10 rows*/
// static lv_color_t buf_2[MY_DISP_HOR_RES * 10];                        /*An other buffer for 10 rows*/
static lv_disp_drv_t mDispDrv;                         /*Descriptor of a display driver*/
static lv_indev_drv_t mKeypadDrv;

static polsys_task_t mLvglTask;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

lv_indev_t * MyIndev;

void LvPort_Init(void)
{
    lv_disp_draw_buf_init(&mDrawBufStruct, mBuff1, NULL, DISP_BUFF_SIZE);   /*Initialize the display buffer*/

    /*-----------------------------------
     * Register the display in LVGL
     *----------------------------------*/
    lv_disp_drv_init(&mDispDrv);                    /*Basic initialization*/
    /*Set the resolution of the display*/
    mDispDrv.hor_res = MY_DISP_HOR_RES;
    mDispDrv.ver_res = MY_DISP_VER_RES;
    /*Used to copy the buffer's content to the display*/
    mDispDrv.flush_cb = DispFlush;
    /*Set a display buffer*/
    mDispDrv.draw_buf = &mDrawBufStruct;
    /*Finally register the driver*/
    lv_disp_drv_register(&mDispDrv);

    // 注册输入设备
    lv_indev_drv_init(&mKeypadDrv);
    mKeypadDrv.type = LV_INDEV_TYPE_KEYPAD;
    mKeypadDrv.read_cb = ReadKeyPad;
    MyIndev = lv_indev_drv_register(&mKeypadDrv);

    // 初始化任务
    Polsys_TaskInit(&mLvglTask, LvglLoop, 10/POLSYS_TICK_CYCLE);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void LvglLoop()
{
    lv_timer_handler();
}

/*Flush the content of the internal buffer the specific area on the display
 *You can use DMA or any hardware acceleration to do this operation in the background but
 *'lv_disp_flush_ready()' has to be called when finished.*/
static void DispFlush(lv_disp_drv_t * mDispDrv, const lv_area_t * area, lv_color_t * color_p)
{
    // /*The most simple case (but also the slowest) to put all pixels to the screen one-by-one*/
    // int16_t x;
    // int16_t y;
    // for(y = area->y1; y <= area->y2; y++) {
    //     for(x = area->x1; x <= area->x2; x++) {
    //         /*Put a pixel to the display. For example:*/
    //         /*put_px(x, y, *color_p)*/
    //         color_p++;
    //     }
    // }
    u32 buff_len = (area->x2 - area->x1 + 1) * (area->y2 - area->y1 + 1) * 2;
    Lcd_SetWindow(area->x1, area->y1, area->x2, area->y2);
    Lcd_FillData((u8*)color_p, buff_len);

    /*IMPORTANT!!!
     *Inform the graphics library that you are ready with the flushing*/
    lv_disp_flush_ready(mDispDrv);
}

/*OPTIONAL: GPU INTERFACE*/

/*If your MCU has hardware accelerator (GPU) then you can use it to fill a memory with a color*/
//static void gpu_fill(lv_disp_drv_t * mDispDrv, lv_color_t * dest_buf, lv_coord_t dest_width,
//                    const lv_area_t * fill_area, lv_color_t color)
//{
//    /*It's an example code which should be done by your GPU*/
//    int32_t x, y;
//    dest_buf += dest_width * fill_area->y1; /*Go to the first line*/
//
//    for(y = fill_area->y1; y <= fill_area->y2; y++) {
//        for(x = fill_area->x1; x <= fill_area->x2; x++) {
//            dest_buf[x] = color;
//        }
//        dest_buf+=dest_width;    /*Go to the next line*/
//    }
//}

// 给LVGL定时调用获取键值和按键状态
static void ReadKeyPad(lv_indev_drv_t * drv, lv_indev_data_t*data)
{
    data->key = KeyConvert((u8)Keyboard_GetLastKey());
    data->state = (lv_indev_state_t)Keyboard_GetLastStatus();
}

// 键值转换
static u8 KeyConvert(u8 key)
{
    switch(key)
    {
        case KEY_OK: return LV_KEY_ENTER;
        case KEY_UP: return LV_KEY_UP;
        case KEY_ESC: return LV_KEY_ESC;
        case KEY_RIGHT: return LV_KEY_NEXT;
        case KEY_DOWN: return LV_KEY_DOWN;
        case KEY_LEFT: return LV_KEY_PREV;
        default : break;
    }
    return 0;
}

